<?php

/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/7/29
 * Time: 14:58
 */
class RebateAction extends BaseAction
{
    function index()
    {
        $point = $this->get_rebate();
        $this->assign('point',$point);
        $sql='select id from score_log where opr_type=20 and uid='.$_SESSION['usersid'].' and to_days(now())=to_days(log_time)';
        $res=db::get_one($sql);
        $this->assign('id',$res->id);
        if(IS_AJAX)return $this->ajax_get($point);

        $sql='select * from score_log where opr_type=20 and uid='.$_SESSION['usersid'].' order by id desc limit 30';
        $list=db::get_all($sql);
        $this->assign('list',$list);

        $this->display('rebate_get');
    }
    function ajax_get($id,$point){
        if($id){
            return $this->result(1,'已经领取过了');
        };
        if($point>0){
                db::_query('begin');
                db::_query('SET AUTOCOMMIT=0');
                $sql='update users set `points`=`points`+'.$point.' where id='.$_SESSION['usersid'];
                if(!db::_query($sql)){
                    db::_query('rollback');
                    return $this->result(1, '失败了,请联系管理员');
                }
                $sql="UPDATE centerbank SET score = score-$point WHERE bankIdx = 9";
                db::_query($sql);
                $sql = "INSERT game_static (uid,typeid,points) values (" . $_SESSION['usersid'] . ",142, $point) ON DUPLICATE KEY UPDATE points=points+'$point'";
                if(!db::_query($sql)){
                    db::_query('rollback');
                    return $this->result(1, '失败了,请联系管理员');
                }
                $sql = "INSERT webtj (time,jjpoints) values (now(),$point) ON DUPLICATE KEY UPDATE jjpoints=jjpoints+'$point'";
                if(!db::_query($sql)){
                    db::_query('rollback');
                    return $this->result(1, '失败了,请联系管理员');
                }
                db::_query('commit');
                db::_query('SET AUTOCOMMIT=1');
                $this->RefreshPoints();
                $this->admin_log(20,$point,$_SESSION['points'],$_SESSION['back'],"每日返利",$_SESSION['usersid']);
                return $this->result(0, '领取成功');
            }else{
                return $this->result(1,"您没有可以领取的返利" );
            }
            return $this->result(1, "出错了");

    }
function get_rebate()
{
    $sql = 'select sum(points) as point from game_day_static where uid=' . $_SESSION['usersid'] . ' and to_days(now())-to_days(time)=1';
    $res = db::get_one($sql,'assoc');
    $point = $res['point'];
    if ($point < 0) {
        $point *= 0.03;
    }else{
        return 0;
    }
    $point=floor(abs($point));
    return $point;
}
}